package com.company.cloud.thirduser.moudules.utils;

import com.company.cloud.thirduser.moudules.properties.TencentSaasProperties;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthRequest;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthResponse;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoRequest;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Component
public class TencentSaasUtil {

    @Autowired
    private TencentSaasProperties properties;

    /**
     * 实名核身鉴权
     * @param   ruleId 用于细分客户使用场景，申请开通服务后，可以在腾讯云慧眼人脸核身控制台
     *          （https://console.cloud.tencent.com/faceid） 自助接入里面创建，审核通过后即可调用
     * @return  人脸核身流程的标识;输出参数参考 https://cloud.tencent.com/document/api/1007/31816
     */
    public String DetectAuth(String ruleId,String redirectUrl) {
        Map<String,String> returnMap = new HashMap<String,String>(2);
        if(StringUtils.isEmpty(ruleId)){
//            logger.error("ruleId不能为空");
            return null;
        }
        String authInfo = null;
        try{
            Credential cred = new Credential(properties.getSecretId(), properties.getSecretKey());

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(properties.getEndpoint());

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            FaceidClient client = new FaceidClient(cred, properties.getRegion(), clientProfile);


            String params = "{\"RedirectUrl\":\""+redirectUrl+"\",\"RuleId\":\""+ruleId+"\"}";
            DetectAuthRequest req = DetectAuthRequest.fromJsonString(params, DetectAuthRequest.class);

            DetectAuthResponse resp = client.DetectAuth(req);
            // 输出 JSON 格式的字符串回包
            authInfo = DetectAuthRequest.toJsonString(resp);


        } catch (TencentCloudSDKException e) {
//            logger.error("活体认证获取token失败:{}",e.toString());
        }
        return authInfo;
    }

    /**
     * 获取实名核身结果信息
     * @param bizToken  人脸核身流程的标识，调用DetectAuth接口时生成。
     * @param ruleId    用于细分客户使用场景
     * @return          输出参数参考 https://cloud.tencent.com/document/api/1007/33560
     */
    public String GetDetectInfo(String bizToken,String ruleId){
        Map<String,String> returnMap = new HashMap<String,String>(2);
        if(StringUtils.isEmpty(ruleId)|| StringUtils.isEmpty(bizToken)){
//            logger.error("bizToken:{},ruleId:{};请检查参数是否为空",bizToken,ruleId);
            return null;
        }

        String detectInfo = null;
        try{
            Credential cred = new Credential(properties.getSecretId(), properties.getSecretKey());

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(properties.getEndpoint());

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            FaceidClient client = new FaceidClient(cred, properties.getRegion(), clientProfile);

            String params = "{\"BizToken\":\""+bizToken+"\",\"RuleId\":\""+ruleId+"\"}";

            GetDetectInfoRequest req = GetDetectInfoRequest.fromJsonString(params, GetDetectInfoRequest.class);

            GetDetectInfoResponse resp = client.GetDetectInfo(req);
            //输出 JSON 格式的字符串回包
            detectInfo = GetDetectInfoRequest.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
//            logger.error(e.toString());
        }
        return detectInfo;
    }


}
